<?php
class bloglistdao extends daobase {
	public function __construct(){
		parent::__construct();
	}
	public function goGetBlogList($blogKind,$userId,$ownerId,$cPage,$pageSize){
		$offset = ($cPage-1)*$pageSize;
		if($userId==$ownerId){
			if($blogKind){
				$sql = "select * from blog where userId='$userId' and kind = '$blogKind'  order by postTime desc limit $offset,$pageSize";
				//echo $sql;
			}else{
				$sql = "select * from blog where userId='$userId' order by postTime desc limit $offset,$pageSize";
			}
		}else{
			if($blogKind){
				$sql = "select * from blog where userId='$userId' and quanxian = '0' and kind = '$blogKind' order by postTime desc limit $offset,$pageSize";
			}else{
				$sql = "select * from blog where userId='$userId' and quanxian = '0' order by postTime desc limit $offset,$pageSize";
			}
		}
		//echo $sql;
		$blogList = $this->fetchit($sql);
		$i=0;
		foreach ($blogList as $key=>$values){
			$userNumber[$i] = $this->goGetUserNumber($values["userId"]);
			$blogKinds[$i] = $this->goGetBlogKind($values["kind"]);
			$i++;
		}
		$list[0]=$blogList;
		$list[1]=$userNumber;
		$list[2]=$blogKinds;
		return $list;
	}
	public function goGetUserNumber($userId){
		$sql = "select user_number from user where id ='$userId'";
		$userNumber =$this->fetchit($sql);
		$userNumber =$userNumber[0][0];
		return $userNumber;
	}
	public function goGetBlogKind($id){
		$sql = "select content from blogkind where id ='$id'";
		//echo $sql;
		$kind = $this->fetchit($sql);
		$kind =$kind[0][0];
		return $kind;
		
	}
	public function goGetBlogKindContent($profileId,$kind,$blogId){
		if ($profileId){
			$sql = "select id from blogkind where content ='$kind' and userID = '$profileId'";
			//echo $sql;
			$kind = $this->fetchit($sql);
			$kind =$kind[0][0];
		}else {
			$sql = "select kind from blog where id = '$blogId'";
			$kind = $this->fetchit($sql);
			$kind = $kind[0][0];
		}
		return $kind;
	}
	public function goGetOneBlog($blogId){
		$sql = "select view from blog where id = '$blogId'";
		$view = $this->fetchit($sql);
		$view = $view[0][0];

		$view++;

		$sql = "update blog set view='$view' where id='$blogId'";
		$updateView = $this->queryit($sql);
		$owner = $_SESSION['userId'];
		$sql = "select * from blog where id='$blogId' order by postTime desc";
		//echo $sql;
		$blogList = $this->fetchit($sql);
		if (count($blogList)&&$blogList[0]['userId']!=$owner&&$blogList[0]['quanxian']=='2'){
			$blogList = array();
		}
		$i=0;
		foreach ($blogList as $key=>$values){
			$userNumber[$i] = $this->goGetUserNumber($values["userId"]);
			$blogKinds[$i] = $this->goGetBlogKind($values["kind"]);
			$i++;
		}
		$list[0]=$blogList;
		$list[1]=$userNumber;
		$list[2]=$blogKinds;
		return $list;
	}
	public function goGetBlogKindList($userId){
		$sql ="select id,content from blogkind where userID ='$userId' order by id asc";
		$kindList =$this->fetchit($sql);	
		return $kindList;
	}
	public function goGetBlogPageList($table,$profileId,$pageSize,$blogKind){
		if($blogKind!=""){
			$sql = "select count(id) from $table where userId='$profileId' and kind = '$blogKind'";
			//echo $sql;
		}else {
			$sql = "select id from blogkind where userID=$profileId and content = '$blogKind'";
			$kindId = $this->fetchit($sql);
			$kindId = $kindId[0][0];
			$sql = "select count(id) from $table where userId='$profileId'";
			//echo $sql;
		}
		//echo $sql;
		//die();
		$pages = $this->fetchit($sql);
		$pages = $pages[0][0];
		//$pageSize=3;
		$pageNumber = ceil($pages/$pageSize);
		//echo $pageNumber;
		//echo 2/3;
		
		return $pageNumber;
	}
	public function getTotalBlogNumber($table,$profileId){
		$sql = "select count(id) from $table where userId='$profileId'";
		$number = $this->fetchit($sql);
		return $number[0][0];
	}
	public function goGetPreviousBlog($blogId,$owner){
		$sql = "select id,title from blog where id > '$blogId' and userId ='$owner' order by postTime asc limit 0,1";
		//echo $sql;
		$result = $this->fetchit($sql);
		return $result;
	}
	public function goGetNextBlog($blogId,$owner){
		$sql = "select id,title from blog where id < '$blogId' and userId ='$owner' order by postTime desc limit 0,1";
		$result = $this->fetchit($sql);
		return $result;
	}
	public function goGetKindBlogNumbers($kind,$userId,$blogId){
		
		if(!$userId){
			$sql = "select userId from blog where id ='$blogId'";
			$userId = $this->fetchit($sql);
			$userId = $userId[0][0];
		}
		
		$sql = "select id from blogkind where userId = '$userId' and content = '$kind'";
		//echo $sql;
		$kindId = $this->fetchit($sql);
		$kindId = $kindId[0][0];
		
		$sql = "select count(id) from blog where userId='$userId' and kind='$kindId'";
		
		$kindNumber = $this->fetchit($sql);
		$kindNumber = $kindNumber[0][0];
		return $kindNumber;
	}
	public function goGetBlogOwner($blogId){
		$sql = "select userId from blog where id ='$blogId'";
		$owner = $this->fetchit($sql);
		$owner = $owner[0][0];
		return $owner;
	}
	public function goSetBlogComment($blogId){
		
	}
	public function goGetBlogComment($blogId){
		$sql = "select * from blogcomment where blogid = '$blogId' order by id asc";
		$comments = $this->fetchit($sql);
		return $comments;
	}
	public function goGetUserInfo($usernumber){
		$sql = "select user_name,image from user where user_number='$usernumber'";
		$userinfo = $this->fetchit($sql);
		return $userinfo[0];
	}
	public function goGetBlogownerByBlogId($blogId){
		$sql = "select userId from blog where id ='$blogId'";
		$userid = $this->fetchit($sql);
		$userid =$userid[0][0];
		$sql = "select user_number from user where id ='$userid'";
		$usernumber = $this->fetchit($sql);
		return $usernumber[0][0];
	}
	public function getBlogTotalNumber($table,$profileId,$kind){
		$expression=new expression("count($table.id) as total");
		$total=$this->select($table,$expression)
		->where("$table.userId='$profileId'");
		if($kind){
			$total->where("$table.kind='$kind'");
		}
		$total=$total->fetchAll();
		return $total[0]['total'];
	}
	public function getBlogKindById($kindId){
		$blogKind = $this->select('blogkind','*')
					->where("id='$kindId'")
					->fetchAll();
		return $blogKind;
	}
	
	
}

?>
